define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods_orders_detail/index' + location.search,
                    add_url: 'goods/goods_orders_detail/add',
                    edit_url: 'goods/goods_orders_detail/edit',
                    del_url: 'goods/goods_orders_detail/del',
                    multi_url: 'goods/goods_orders_detail/multi',
                    import_url: 'goods/goods_orders_detail/import',
                    table: 'goods_orders_detail',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='goods']", form).addClass("selectpage").data("source", "goods/goods/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                $("input[name='spec']", form).addClass("selectpage").data("source", "goods/goods_spec/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                $(form).on("reset", function() {
                    $('.selectpage', form).selectPageClear();
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_id', title: __('Order_id'),visible: false},
                        {
                            field: 'goods_id', title: __('Goods_id'),
                            formatter: function (value, row, index) {
                                if (row.goods === null){
                                    test = '无';
                                }else{
                                    test = row.goods.name;
                                }
                                return '<a href="javascript:;" class="searchit btn-goods_id" data-toggle="tooltip" data-original-title="点击搜索 '+test+'"> ' + test + '</a>';
                            },
                            events: {
                                //格式为：方法名+空格+DOM元素
                                'click .btn-goods_id': function (e, value, row, index) {
                                    e.stopPropagation();
                                    var container = $("#table").data("bootstrap.table").$container;
                                    //这里我们手动将数据填充到表单然后提交
                                    $("form.form-commonsearch [name='goods_id']", container).val(value);
                                    $("form.form-commonsearch", container).trigger('submit');
                                }
                            },
                        },
                        {
                            field: 'spec_id', title: __('Spec_id'),
                            formatter: function (value, row, index) {
                                if (row.spec === null){
                                    test = '无';
                                }else{
                                    test = row.spec.name;
                                }
                                return '<a href="javascript:;" class="searchit btn-spec_id" data-toggle="tooltip" data-original-title="点击搜索 '+test+'"> ' + test + '</a>';
                            },
                            events: {
                                //格式为：方法名+空格+DOM元素
                                'click .btn-spec_id': function (e, value, row, index) {
                                    e.stopPropagation();
                                    var container = $("#table").data("bootstrap.table").$container;
                                    //这里我们手动将数据填充到表单然后提交
                                    $("form.form-commonsearch [name='spec_id']", container).val(value);
                                    $("form.form-commonsearch", container).trigger('submit');
                                }
                            },
                        },
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'num', title: __('Num')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
